package com.jyh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jyh.common.CustomException;
import com.jyh.common.PublicField;
import com.jyh.common.R;
import com.jyh.pojo.ShoppingCart;
import com.jyh.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 显示购物车
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list(){
        Long id = PublicField.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,id);
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return R.success(list);
    }

    /**
     * 添加到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //log.info(shoppingCart.toString());
        //设置创建人id
        shoppingCart.setUserId(PublicField.getCurrentId());

        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();

        Long dishId = shoppingCart.getDishId();

        if(dishId==null){
            //套餐
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else {
            //菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }

        ShoppingCart serviceOne = shoppingCartService.getOne(lqw);

        if(serviceOne==null){
            //第一次添加
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //新建
            shoppingCartService.save(shoppingCart);
            serviceOne=shoppingCart;

        }else{
            //多份
            Integer number = serviceOne.getNumber();
            serviceOne.setNumber(number+1);
            //更新
            shoppingCartService.updateById(serviceOne);
        }

        return R.success(serviceOne);
    }

    /**
     * 减少数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("sub")
    public R<ShoppingCart> deleteOne(@RequestBody ShoppingCart shoppingCart){
        //log.info(shoppingCart.toString());
        Long currentId = PublicField.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);

        Long dishId = shoppingCart.getDishId();
        if(dishId==null){
            //套餐
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else{
            //菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }
        //根据userID和dishId或setmealId查找
        ShoppingCart serviceOne = shoppingCartService.getOne(lqw);

        if(serviceOne==null){
            //说明购物车中没有商品
            throw  new CustomException("购物车中不存在该商品");
        }

        Integer number = serviceOne.getNumber();
        if(number>1){
            //多份时
            serviceOne.setNumber(number-1);
            shoppingCartService.updateById(serviceOne);
        }else{
            //只有一份时，删除该商品
            shoppingCartService.removeById(serviceOne);
        }

        return R.success(serviceOne);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long currentId = PublicField.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw= new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(lqw);
        return R.success("success");
    }

}
